<template>
    <div id="space">
        <div style="display: flex;justify-content: space-between;align-items: center;">
            <div style="color: #000000;font-weight: bold;" class="s-20">空间管理</div>
            <div @click="goback"  class="back">
                <img  src="../../../assets/img/person/back.png" alt="">
                <span class="s-15 pointer">返回</span>
            </div>
        </div>
        <div class="spaceSize">
            <div class="progress">
                <el-progress style="width:56vw" :percentage="spacePercent" :stroke-width="20" :show-text="false"></el-progress>
                <div class="usedSize">
                    <span style="margin-left: 20px;">已使用 :</span>
                    <span style="color: #0085FF;">{{totalSize > 512 ? ((usedSize/1024).toFixed(3) + 'G') : (usedSize+'M')}}</span>
                    <span>/{{totalSize > 512 ? ((totalSize/1024).toFixed(1) + 'G') : (totalSize+'M')}}</span>
                    <span style="margin-left: 10px;font-weight: bold;color: #F7B500;">{{'('+spacePercent+'%)'}}</span>
                </div>
            </div>
        </div>
        <div class="spaceFile">
                <el-table :data="uploadFileData" style="width: 100%;"  :header-cell-style="headerStyle">
                    <el-table-column label="文件名" width="450">
                        <template slot-scope="scope" >
                            <div style="white-space: nowrap;display: flex;align-items: center;">
                                <img v-if=" scope.row.fileType == '1' " style="width: 23px;height: 26px;" src="../../../assets/img/cloud/ppt.png" alt="">
                                <img v-if=" scope.row.fileType == '2' " style="width: 23px;height: 26px;" src="../../../assets/img/cloud/pdf.png" alt="">
                                <img v-if=" scope.row.fileType == '3'"  style="width: 23px;height: 26px;"  src="../../../assets/img/cloud/word.png" alt="">
                                <img v-if=" scope.row.fileType == '4'"  style="width: 24px;height: 24px;"  src="../../../assets/img/cloud/img.png" alt="">
                                <img v-if=" scope.row.fileType == '5'"  style="width: 26px;height: 23px;"  src="../../../assets/img/cloud/video.png" alt="">
                                <img v-if=" scope.row.fileType == '6'"  style="width: 24px;height: 24px;"  src="../../../assets/img/cloud/audio.png" alt="">
                                <div class="s-14 " style="margin-left: 8px;;color: #303133;">
                                    <el-popover trigger="hover" placement="top"  align="left" :disabled="scope.row.displayName.length <= 30">
                                        <div>
                                            {{ scope.row.displayName }}
                                        </div>
                                        <div slot="reference" >
                                            <div style="max-width: 400px;overflow: hidden;text-overflow: ellipsis;padding-top: 5px;"><span class="pointer line-over-ellipsis">{{ scope.row.displayName }}</span></div>
                                        </div>
                                    </el-popover>
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="大小">
                        <template slot-scope="scope">
                            <div>
                                <span style="font-weight: 400; font-size: 14px">{{scope.row.fileSize }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="类型">
                        <template slot-scope="scope">
                            <div>
                                <span style="font-weight: 400; font-size: 14px">{{scope.row.typeName }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="上传时间" >
                        <template slot-scope="scope">
                            <div style="white-space: nowrap;margin-right: 30px;">
                                <span style="font-weight: 400; font-size: 14px;">{{scope.row.createTime }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="200">
                        <template slot-scope="scope">
                            <div style="display: flex;align-items: center;justify-content: center;" >
                                <el-popover trigger="click" placement="top"  align="left" >
                                    <div v-if="scope.row.relation.length > 0">
                                        <div v-for="(item , index) in scope.row.relation" :key="index" style="text-align:center ;margin: 5px 0 ;font-size: 14px;">
                                            {{item.lessonName}}
                                        </div>
                                    </div>
                                    <div v-else style="text-align:center ;">
                                        暂无关联课程
                                    </div>
                                    <div slot="reference" >
                                        <div style="color: #4893ff;" @click="lookRelevance(scope.row)"><span class="pointer">查看关联课程</span></div>
                                    </div>
                                </el-popover>
                                <div style="color: #4893ff;margin-left: 20px;" @click="deleteFile(scope.row)"><span class="pointer">删除</span></div>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination">
                    <div>
                        共
                        <span style="font-weight: bold;">{{ total }}</span>
                        个文件
                    </div>
                    <el-pagination
                        background
                        layout="prev, pager, next"
                        :page-size="pageSize"
                        :total="total"
                        @current-change="handleChange"
                        >
                    </el-pagination>
                </div>
        </div>
    </div>
</template>

<script>
import { SqlLibBook } from "../../../../../../static/lib/sqlite/service/SqlLibBook";
import { SqlLib } from '../../../../../../static/lib/sqlite/SqlLib'
import { SqlLibUser } from '../../../../../../static/lib/sqlite/service/SqlLibUser'
import resource from '@/content/api/resource'
import usermeta from '@/content/api/usermeta'
export default {
    name:'',
    components: {},
    data() {
    return {
        totalSize:0,
        usedSize:0,
        spacePercent:0,
        uploadFileData:[],
        pageNum:1,
        pageSize:12,
        total:0,
    };
    },
    computed: {},
    watch: {},
    methods: {
        handleChange(val){
            this.pageNum = val
            this.getfileData()
        },
        lookRelevance(row){
            console.log(row);
        },
        deleteFile(row){
            this.$dialog({
                confirm: '确认',
                cancel: '取消',
                message: '是否确认删除该文件?',
                close: false,
                type: 'warn',
                title:'温馨提示'
            }).then(()=>{
                switch (row.fileType) {
                    case '1':
                        
                        break;
                    case '2':
                        
                        break;
                    case '3':
                        
                        break;
                    case '4':
                        
                        break;
                    case '5':
                        
                        break;
                    default:
                        break;
                }
                let user = SqlLibUser.queryCurrentUser(SqlLib.getInstance(window.$DBDIR));
                if (row.relation.length > 0) {
                    row.relation.forEach(item => {
                        let tempName = item.lessonId + '-' +row.displayName
                        let userResource = SqlLibBook.queryOneResourceByName(SqlLib.getInstance(window.$DBDIR), user.id, item.lessonId,tempName)
                        
                        if (userResource) {
                            userResource.reverse3 = '1'
                            SqlLib.getInstance(window.$DBDIR).updateOrInsertObjectById(userResource)
                            console.log('userResource',userResource);
                        }
                    });
                }
                // return
                resource.deleteUserUploadFile({
                    fileId:row.fileId
                }).then(res=>{
                    console.log('1111111111111111' ,res);
                    if (res.code == '0') {
                        this.$message.success('删除成功')   
                        this.getfileData()  
                        this.getUserSpace()             
                    }
                })
            }).catch(()=>{
            })
        },
        headerStyle(cell) {
            return 'font-weight: 550;color: rgba(0,0,0,0.85);background: #EBF1F7;height:48px  '
        },
        formatName(name){
            if(name.lastIndexOf('.') > 0){
                let newName = name.substring(0,name.lastIndexOf('.'))
                return newName
            }
        },
        getfileData(){
            resource.getAllUploadFile({
                includeCourseware:'1',
                pageNum:this.pageNum,
                pageSize:this.pageSize,
            }).then(res=>{
                if (res.code == '0') {
                    this.total = Number(res.returnValue.total)
                    this.uploadFileData = res.returnValue.records
                    this.uploadFileData.forEach(item=>{
                        switch (item.fileType) {
                            case '1':
                                item.typeName = '文档'
                                break;
                            case '2':
                                item.typeName = '文档'
                                break;
                            case '3':
                                item.typeName = '文档'
                                break;
                            case '4':
                                item.typeName = '图片'
                                break;
                            case '5':
                                item.typeName = '视频'
                                break;
                            case '6':
                                item.typeName = '音频'  
                                break;
                            default:
                                break;
                        }
                        let timeIndex = item.createTime.indexOf(':')
                        item.createTime = item.createTime.substring(0 , timeIndex -3)
                        if (item.fileSize > 1024*1024) {
                            item.fileSize = (item.fileSize/1024/1024).toFixed(1) + 'MB'
                        }else{
                            item.fileSize = (item.fileSize/1024).toFixed(1) + 'KB'
                        }
                    })
                }
            })
        },
        getUserSpace(){//获取上传空间大小
            resource.getUserSpace({
                appKey:'NOVO_EDU_BEAUTY_SCIENCE_PC'
            }).then( res =>{
                if (res.code == '0') {
                    this.totalSize = res.returnValue.totalSize 
                    this.usedSize =  res.returnValue.usedSize
                    localStorage.setItem('spaceSize',res.returnValue.totalSize)
                    if(res.returnValue.usedSize > res.returnValue.totalSize){
                        localStorage.setItem('spaceusedSize',res.returnValue.totalSize) 
                    }else{
                        localStorage.setItem('spaceusedSize',res.returnValue.usedSize) 
                    }
                    this.spacePercent = Number(((this.usedSize/this.totalSize)*100).toFixed(1)) 
                }
            })
        },
        goback () {
            let sqlLib = SqlLib.getInstance(window.$DBDIR)
            let user = SqlLibUser.queryCurrentUser(sqlLib)
            let obj = {
                bookId: user.bookId
            }
            if (this.authorzation) {
                if (user.lessonId) {
                    obj.lessonId = user.lessonId
                } else {
                    let book = SqlLibBook.queryChapterLessonByBookId(sqlLib, user.bookId)
                    if (book) {
                        obj.lessonId = book[0].lessonVO[0].id
                    }
                }
            }
            this.$router.push({ name: 'home', params: this.authorzation ? obj : {} })
        },
    },
    created() {
    
    },
    mounted() {
        this.totalSize = Number(localStorage.getItem('spaceSize'))
        this.usedSize = Number(localStorage.getItem('spaceusedSize'))
        if (Number(this.usedSize)  >= Number(this.totalSize)) {
            console.log('满了');
            this.usedSize = this.totalSize
        }
        // console.log(this.usedSize,((this.usedSize/this.totalSize)*100).toFixed(3));
        this.spacePercent = Number(((this.usedSize/this.totalSize)*100).toFixed(this.totalSize > 512 ? 2 : 1)) 
        this.getfileData()
    },
}
</script>
<style scoped lang="scss">
#space{
    padding: 20px 30px;
    width: 100%;
    .back {
        position: absolute;
        right: 30px;
        color: #606266;
        cursor: pointer;
        width: 88px;
        height: 32px;
        display: flex;
        justify-content: center;
        align-items: center;
        background: #0085FF;
        color: #FFFFFF;
        border-radius: 4px;
        border: 1px solid #D7D7D7 !important;
        img{
            width: 8px;
            height: 11px;
            margin-right: 6px;
        }
    }
    .spaceSize{
        margin: 20px 0 30px;
        text-align: center;
        .usedSize{
            margin-right: 0px;
        }
    }
    .progress{
        display: flex;
        justify-content: space-between;
        font-weight: bold;
    }
    .spaceFile{
        width: calc(100vw - 340px);
        .reletionCourse{
            position: absolute;
            top: -100px;
        }
        .pagination{
            // padding: 0 30px;
            position: absolute;
            bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center; 
            width: calc(100% - 330px);
        }
    }
}
</style>
<style scope lang="scss">
#space{
    .el-progress-bar .el-progress-bar__outer {
        border-radius: 4px;
    }
    .el-progress-bar__inner {
        border-radius: 4px; // 双边圆角
        background: linear-gradient(180deg, #34EBD3 0%, #18D2A8 100%);
    }
    .btn-prev , .btn-next{
            background-color: #fff;
            line-height: 30px;
            height: 30px;
            min-width: 30px;
        }
        .el-pager li{
            background-color: #fff;
            line-height: 30px;
            height: 30px;
            min-width: 30px;
        }
        .el-pager li.active{
            // background: none!important;
            border: 1px solid #4893ff;
            color: #4893ff;
    }
}
.spaceFile{
    .el-table .el-table__cell{
        padding: 6px 0;
    }
}
</style>